using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SmartContentUI.Document
{
    public partial class ViewFileNav : UserControl
    {
        #region Variables
        private SmartContentBusiness.Session ses;
        private SmartContentDB.Interfaces.Content curContent;
        private int registryId;
        #endregion

        #region Delegates
        public delegate void OpenFile(string path);
        public event OpenFile Open;
        #endregion

        #region Constructor
        public ViewFileNav(ImageList imgLst, SmartContentBusiness.Session ses, SmartContentDB.Interfaces.Content content)
        {
            InitializeComponent();
            this.treeView1.ImageList = imgLst;
            this.ses = ses;
            this.curContent = content;
        }
        #endregion

        #region Functions
        public void Clear()
        {
            this.treeView1.Nodes.Clear();
        }

        public void setContent(SmartContentDB.Interfaces.Content content)
        {
            this.curContent = content;
        }

        public void loadTree(int registryId)
        {
            this.registryId = registryId;
            this.treeView1.Nodes.Clear();
            SmartContentDB.Interfaces.Content[] content = ses.getRegistryContent(curContent.getTableName(), registryId, -1);
            if (content != null)
            {
                for (int i = 0; i < content.Length; i++)
                {
                    TreeNode tvNode = new TreeNode(content[i].getName(), 2, 2);
                    tvNode.Tag = content[i].getId();
                    tvNode.Name = content[i].getTableName();
                    tvNode.ToolTipText = content[i].getDescription();
                    loadSubTree(tvNode);
                    this.treeView1.Nodes.Add(tvNode);
                }
            }
        }

        private void loadSubTree(TreeNode parent)
        {
            if(parent.ImageIndex == 2)
            {
                SmartContentDB.Interfaces.Content[] content = ses.getRegistryContent(curContent.getTableName(), registryId, (int)parent.Tag);

                if (content != null)
                {
                    for (int i = 0; i < content.Length; i++)
                    {
                        TreeNode tvNode = new TreeNode(content[i].getName(), content[i].getType() == SmartContentDB.Interfaces.Content.ContentType.Cab ? 2 : 1,
                            content[i].getType() == SmartContentDB.Interfaces.Content.ContentType.Cab ? 2 : 1);
                        if(content[i].getType() == SmartContentDB.Interfaces.Content.ContentType.Cab)
                            tvNode.Tag = content[i].getId();
                        else
                            tvNode.Tag = content[i].getDirectory();
                        tvNode.Name = content[i].getTableName();
                        tvNode.ToolTipText = content[i].getDescription();
                        loadSubTree( tvNode);
                        parent.Nodes.Add(tvNode);
                    }
                }
            }
        }
        #endregion

        #region Events
        private void addDirBtn_Click(object sender, EventArgs e)
        {
            string data = Util.OptionPane.InputMessage("Crear carpeta", "Digite el nombre de la carpeta", SmartContentUI.Util.OptionPane.InputType.String);
            if (data != null)
            {
                if (treeView1.Nodes.Count == 0)
                {
                    TreeNode tvNode = new TreeNode(data, 2, 2);
                    this.treeView1.Nodes.Add(tvNode);
                    this.ses.addRegistryContent(curContent.getTableName(), registryId, new SmartContentDB.Interfaces.Content(-1, -1, data, "", SmartContentDB.Interfaces.Content.ContentType.Cab, null, null));
                }
            }
        }

        private void folderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode current = this.treeView1.SelectedNode;
            string data = Util.OptionPane.InputMessage("Crear carpeta", "Digite el nombre de la carpeta", SmartContentUI.Util.OptionPane.InputType.String);
            if (data != null)
            {
                TreeNode tvNode = new TreeNode(data, 2, 2);
                current.Nodes.Add(tvNode);
                this.ses.addRegistryContent(curContent.getTableName(), registryId, new SmartContentDB.Interfaces.Content(-1, (int)current.Tag, data, "", SmartContentDB.Interfaces.Content.ContentType.Cab, null, null));
            }
        }

        private void archivoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode current = this.treeView1.SelectedNode;
            OpenFileDialog file = new OpenFileDialog();
            DialogResult res = file.ShowDialog();
            string name;
            if (res == DialogResult.OK)
            {
                if (curContent.getDirectory() != null)
                {
                    if(curContent.getDirectory().Trim().Length == 0)
                    {
                        MessageBox.Show("No hay un volumen definido para el expediente", "Error agregando archivo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    if (!file.CheckFileExists)
                    {
                        MessageBox.Show("Archivo no existe", "Agregar Archivo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    try
                    {
                        name = file.FileName.Substring(file.FileName.LastIndexOf("\\") + 1);
                        if (!System.IO.Directory.Exists(curContent.getDirectory() + @"\" + registryId))
                            System.IO.Directory.CreateDirectory(curContent.getDirectory() + @"\" + registryId);
                        System.IO.File.Copy(file.FileName, curContent.getDirectory() + @"\" + registryId + @"\" + name, true);
                        TreeNode tvNode = new TreeNode(name, 1, 1);
                        tvNode.Tag = curContent.getDirectory() + @"\" + registryId + @"\" + name;
                        current.Nodes.Add(tvNode);
                        this.ses.addRegistryContent(curContent.getTableName(), registryId, new SmartContentDB.Interfaces.Content(-1, (int)current.Tag, name, "", SmartContentDB.Interfaces.Content.ContentType.File, null, curContent.getDirectory() + @"\" + registryId + @"\" + name));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error al agregar archivo: " + ex.Message, "Error agregando archivo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    MessageBox.Show("No hay un volumen definido para el expediente", "Error agregando archivo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }
        }
        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            if (this.treeView1.SelectedNode.ImageIndex == 1)
                Open((string)this.treeView1.SelectedNode.Tag);
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            TreeNode tv = this.treeView1.SelectedNode;
            if (tv != null)
            {
                if (tv.ImageIndex == 2)
                {
                    this.agregarToolStripMenuItem.Enabled = true;
                }
                else
                {
                    this.agregarToolStripMenuItem.Enabled = false;
                }
            }
        }

        private void borrarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode current = this.treeView1.SelectedNode;
        }
        #endregion

        
    }
}
